package com.mapbased.wfw.bind;

import com.mapbased.wfw.RenderContext;
import com.mapbased.wfw.PageHandler;
import com.mapbased.wfw.Page;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class OPageBinding
    extends AbstractBinding
{
    private java.lang.reflect.Method method;
    private boolean isContext;
    /**
     * the page in which the component itself
     */
    private Page page;

    public OPageBinding(String exp)
    {
        super(exp);

        //   this.attributeName=attributeName;
    }

    public void setPage(Page page)
    {
        this.page=page;
    }

    /**
     * getValue
     *
     * @param context RenderContext
     * @return Object
     * @todo Implement this com.mapbased.wfw.Binding method
     */
    protected Object getDirectValue(RenderContext context)
    {
        try
        {

            PageHandler handler=this.page.getPageHandler();

            if(method == null)
            {
                synchronized(this)
                {

                    try
                    {
                        method=handler.getClass().getMethod(this.
                            attributeName.trim() + "Binding",
                            new Class[]
                            {RenderContext.class});
                        this.isContext=true;

                    }

                    catch(NoSuchMethodException ex)
                    {
                        method=handler.getClass().getMethod(this.
                            attributeName.trim() + "Binding",
                            new Class[]
                            {java.util.Map.class});
                        this.isContext=false;

                    }
                }
            }

            Object o=context;
            if(!this.isContext)
            {
                o=context.getRequestMap();
            }
            return method.invoke(handler,new Object[]
                                 {o});
        }

        catch(Exception e)
        {
            throw new java.lang.RuntimeException(e);
        }
    }

}
